import datetime
import os
from pathlib import Path
from typing import Union
from loguru import logger


def time_format(t: datetime.datetime) -> str:
    logger.info(t)
    return t.strftime('%Y_%m_%d_%H_%M_%S_%f')


# 获取项目根目录
def get_project_path() -> Path:
    return Path(__file__).parent


# 创建目录树
def create_folders(p: Union[Path, str]):
    p = Path(p)
    p.mkdir(parents=True, exist_ok=True)


def adb_kill_process_id(process_id: int):
    if process_id == -1:
        return
    os.system(f'adb shell kill {process_id}')


def adb_get_process_id(package_name: str) -> int:
    source = os.popen(f'adb shell top -n 1 -b | findstr "{package_name}"').read()
    if len(source) == 0:
        return -1
    return int(source.split()[0])


def adb_kill_package(package_name: str):
    p_i = adb_get_process_id(package_name)
    adb_kill_process_id(p_i)


if __name__ == '__main__':
    logger.add('run.log')
    adb_kill_package('com.example.stresstest')
